﻿using System;
using System.Data;
using System.Text;
using ZHDJ.Core;
using ZHDJ.Core.Caching;
using ZHDJ.Data;
using ZHDJ.Data.DataCaching;

namespace ZHDJ.Web.PortalParts
{
    public partial class MenuPortal : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadMenu();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public string MenuPath
        {
            get
            {
                var s = (string)ViewState["__MenuPath__"];
                return s ?? string.Empty;
            }
            set
            {
                ViewState["__MemuPath__"] = value;
            }
        }

        private MemoryCacheManager mCache = new MemoryCacheManager();
        private void LoadMenu()
        {
            nav.InnerHtml = mCache.Get<string>(CacheKeys.Cache_Menu, 180, () =>
            {
                DataTable dtMenu = MySqlHelper.ExecuteDataTable("select * from tblportalmenu order by OrderNo");
                TraverseMenu(dtMenu, dtMenu.Select("(ParentID='' or ParentID is null)"));
                return menu;
            });
        }
        public string menu = "";
        /// <summary>
        /// 遍历菜单
        /// </summary>
        /// <param name="source"></param>
        /// <param name="dataRow"></param>
        private void TraverseMenu(DataTable source, DataRow[] dataRow)
        {

            if (dataRow.Length > 0)
            {
                menu += string.IsNullOrEmpty(menu) ? "<ul class='sf-menu'>" : "<ul>";
                foreach (DataRow item in dataRow)
                {
                    var hrefValue = Convert.ToString(item["NavigateUrl"]);
                    //if (Convert.ToString(item["Name"]) == "入党申请")
                    //{
                    //    hrefValue = "#\"" + " onclick=\"joinprompt()";
                    //}

                    menu += string.Format("<li><a href=\"{0}\">{1}</a>", hrefValue, Convert.ToString(item["Name"]));
                    TraverseMenu(source, source.Select("ParentID='" + Convert.ToString(item["ID"]) + "'"));
                    menu += "</li>";
                }
                menu += "</ul>";
            }
        }
    }
}